using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Api.Api.Controllers.Tables
{
    [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators })]
    public class DictionarysController : BaseController
    {
        private readonly IDictionarysServer _iDictionarysServer;

        public DictionarysController(IDictionarysServer iDictionarysServer)
        {
            _iDictionarysServer = iDictionarysServer;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog("添加字典")]
        public async Task<IActionResult> Add(RequestCreateDictionarysDto dto)
        {
            return Send(await _iDictionarysServer.AddAsync(dto));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog("修改字典")]
        public async Task<IActionResult> Update(RequestUpdateDictionarysDto dto)
        {
            return Send(await _iDictionarysServer.UpdateAsync(dto));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog("删除字典")]
        public async Task<IActionResult> Delete(RequestDeleteDictionarysDto dto)
        {
            return Send(await _iDictionarysServer.DeleteAsync(dto));
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Detail(int id)
        {
            return Send(await _iDictionarysServer.DetailAsync(id));
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page">当前是第几页</param>
        /// <param name="limit">一页有多少条数据</param>
        /// <param name="key">要查询的关键字</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Pagination(int page, int limit, string? key)
        {
            return Send(await _iDictionarysServer.PaginationAsync(page, limit, key));
        }
        /// <summary>
        /// 获取第一层子级通过类别编号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSunDictionaryByDTId(int id)
        {
            return Send(await _iDictionarysServer.GetSunDictionaryByDTId(id));
        }
        /// <summary>
        /// 获取子级通过父级编号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSunDictionaryByParentId(int id)
        {
            return Send(await _iDictionarysServer.GetSunDictionaryByParentId(id));
        }

        /// <summary>
        /// 通过键值获取字典树
        /// </summary>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDictionaryBySearchKey(string searchKey)
        {
            return Send(await _iDictionarysServer.GetDictionaryBySearchKey(searchKey));
        }


    }
}
